<?php
// 本类由系统自动生成，仅供测试用途
class GoodsAction extends Action {
    public function index(){
    	// $Goods=M('Goods');
    	// Import('ORG.Util.Page');
    	// $count=$Goods->count();
    	// $page=new Page($count,10);
    	// $show=$page->show();
    	// $rows=$Goods->limit($page->firstRow.','.$page->listRows)->select();
    	// $this->assign('show',$show);
    	// $this->assign('rows',$rows);
    	// $this->display();
    	$db=M();
    	Import('ORG.Util.Page');
    	$sql1="select count(*) tot from group_goods";
    	$tot=$db->query($sql1);
    	$count=$tot[0]['tot'];
    	$page=new Page($count,10);
    	$show=$page->show();
    	$sql2="select g.*,s.shopname,c.cname,l.lname from group_goods g
    				left join group_shop s on g.shopid=s.shopid
    				left join group_category c on g.cid=c.cid
    				left join group_locality l on g.lid=l.lid
    				limit {$page->firstRow},{$page->listRows}
    				";
    	$rows=$db->query($sql2);
    	$this->assign('rows',$rows);
    	$this->assign('show',$show);
    	$this->display();
	}
	// 获取所有商品服务
	public  function getServer(){
		$arr=array('假一赔十','7天无理由退换','不支持7天无理由退换','延保一年');
		return $arr;
	}

	// 添加分类
	public function add(){
		// 商铺信息
		$shopid=$_GET['shopid'];
		$shoprow=D('Shop')->getData($shopid);
		// 商铺管理-添加商品过来直接显示商铺名称，在商品管理-添加商品选择商铺
		if ($shopid == true) {
			$this->assign('shoprow',$shoprow);
		}else{
			$shoprows=D('Shop')->getDataAll();
			$this->assign('shoprows',$shoprows);
		}
		// 商品服务
		$serverrows=$this->getServer();
		$this->assign('serverrows',$serverrows);
		
		// 分类信息
		$caterows=D('Category')->getDataAll();
		$this->assign('caterows',$caterows);
		// 地区信息
		$localrows=D('Locality')->getDataAll();
		$this->assign('localrows',$localrows);
		// pre($localrows);exit;

		$this->assign('rows',$rows);
		$this->display();
	}
	// 上传图片
	private function upload(){
		import('ORG.Net.UploadFile');
		import('ORG.Util.Image');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath =  './Public/Uploads/';// 设置附件上传目录
		$upload->thumbRemoveOrigin = true;//是否删除原图
		$upload->thumb = true;//是否创建缩略图
		$upload->thumbPrefix = '460x280_,200x100_,310x185_,90x55_';//缩略图前缀生成几个缩略图加几个前缀
		$upload->thumbMaxWidth = '460,200,310,90';//缩略图最大宽度
		$upload->thumbMaxHeight = '280,100,185,55';//缩略图最大高度

		if(!$upload->upload()) {// 上传错误提示错误信息
		$this->error($upload->getErrorMsg());
		}else{// 上传成功 获取上传文件信息
		$info =  $upload->getUploadFileInfo();
		// $savename=$info[0]['savename'];
		return $info;
		}
	}
	// 添加分类操作
	public function add_handle(){
		// 上传图片
		$info=$this->upload();//
		// pre($info);exit;
		// 上传图片结束
		$_POST['goods_img']=$info[0]['savename'];
		// pre($_POST);exit;
		$Goods=M('Goods');
		if($Goods->create()){
			$gid=$Goods->add();
			$data['goods_id']=$gid;
			$data['detail']=$_POST['detail'];
			$data['goods_server']=serialize($_POST['goods_server']);
			$Goods_detail=M('Goods_detail');
			if($Goods_detail->add($data)){
				$this->redirect('index');
			}
		}else{
			echo '出错了！';
		}
	}

	// 编辑
	public function edit(){
		$gid=$_GET['gid'];
		$db=M();
		$sql="select * from group_goods g,group_goods_detail d where g.gid={$gid} and g.gid=d.goods_id";
		$datarow=$db->query($sql);
		$row=$datarow[0];
		// pre($row);exit;
		$goods_server=unserialize($row['goods_server']);
		// 商品服务返序列化
		$this->assign('goods_server',$goods_server);
		$this->assign('row',$row);
		// 商品服务
		$serverrows=$this->getServer();
		$this->assign('serverrows',$serverrows);
		// 商铺信息
		$shoprows=D('Shop')->getDataAll();
		$this->assign('shoprows',$shoprows);
		// 分类信息
		$caterows=D('Category')->getDataAll();
		$this->assign('caterows',$caterows);
		// 地区信息
		$localrows=D('Locality')->getDataAll();
		$this->assign('localrows',$localrows);
		$this->display();
	}
	// 编辑操作
	public function edit_handle(){
		$Goods=M('Goods');
		$info=$this->upload();
		$_POST['goods_img']=$info[0]['savename'];
		// 删除原图
		$arr=array('200x100_','310x185_','460x280_','90x55_');//缩略图前缀
		foreach ($arr as $k => $v) {
			$old_img=$info[0]['savepath'].$v.$_POST['old_img'];
			unlink($old_img);
		}
		if($Goods->create()){
			$Goods->save();//修改商品表
			$data['detail']=$_POST['detail'];
			$data['goods_server']=serialize($_POST['goods_server']);
			$goods_detail=M('Goods_detail');
			$goods_detail->where('goods_id='.$_POST['gid'])->save($data);//修改商品细节表
			$this->redirect('index');
		}

			

	}

	// 删除
	public function delete(){
		$Goods=M('Goods');
		$goods_detail=M('Goods_detail');
		$gid=$_GET['gid'];
		$gre=$Goods->where('gid='.$gid)->delete();
		$dre=$goods_detail->where('goods_id='.$gid)->delete();
		// 删除原图
		$img=$_GET['img'];
		$arr=array('200x100_','310x185_','460x280_','90x55_');//缩略图前缀
		foreach ($arr as $k => $v) {
			$old_img='./Public/Uploads/'.$v.$img;
			unlink($old_img);
		}
		if($gre){
			$this->redirect('index');
		}
	}
		

}